/**
 *
 * Copyright 2009 garrett
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License.  You may obtain a copy of the
 * License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License. 
 */
package org.memesis.pandorasbook.junit;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.memesis.pandorasbook.PandoraApplet;
import org.memesis.pandorasbook.PandoraAppletFactory;
import org.xml.sax.SAXException;

/**
 * Test cases for PandoraAppletFactory
 *
 * @author garrett
 */
public class PandoraAppletFactoryTest {

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link org.memesis.pandorasbook.PandoraAppletFactory#createApplet(java.net.URL, org.memesis.pandorasbook.PandoraDesktop)}.
	 */
	@Test
	public void testMockApplet() {
		try {
			File f = File.createTempFile("applet", ".xml");
			PrintWriter out = new PrintWriter(new FileWriter(f));
			out.println("<pandoras-book>");
			out.println("<applet type=\"junit.MockApplet\" />");
			out.println("<property name=\"test\" value=\"Hello World\"/>");
			out.println("</pandoras-book>");
			out.flush();
			out.close();
			
			URL appletUrl = f.toURI().toURL();
			
			PandoraApplet applet = PandoraAppletFactory.createApplet(appletUrl, null);
			
			assertNotNull("Applet was null", applet);
			assertTrue("Properties for the applet did not contain an origin URL", applet.getProperties().containsKey("pandorasbook-applet-url"));
			assertEquals("Applet origin URL did not match expected", appletUrl.toString(), applet.getProperty("pandorasbook-applet-url"));
			
			f.delete();
		} catch (IOException e) {
			fail("Exception during applet creation: " + e);
		} catch (ParserConfigurationException e) {
			fail("Exception during applet creation: " + e);
		} catch (SAXException e) {
			fail("Exception during applet creation: " + e);
		} catch (ClassNotFoundException e) {
			fail("Exception during applet creation: " + e);
		} catch (InstantiationException e) {
			fail("Exception during applet creation: " + e);
		} catch (IllegalAccessException e) {
			fail("Exception during applet creation: " + e);
		}
	}

	/**
	 * Test method for {@link org.memesis.pandorasbook.PandoraAppletFactory#createApplet(java.net.URL, org.memesis.pandorasbook.PandoraDesktop)}.
	 */
	@Test
	public void testBsfApplet() {
		//fail("Not yet implemented");
	}
}